package CuratorCtrl;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PortManage extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 548740008617540763L;
	private JComboBox cbox_rules;
	private JPanel jPanel1;
	private JLabel jLabel3;
	private JTextField tfHost;
	private JLabel jLabel1;
	private JTextField tfGuest;
	private JButton bu_apply;
	private JPanel jPanel2;
	private rule[] rulelist;
	private String name="alaph";

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				PortManage inst = new PortManage();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public PortManage(String name_in) throws IOException {
		super();
		this.name = name_in;
		rulelist = Util.getrules(name);
		initGUI();
	}

	public PortManage() {
		super();
		try {
			rulelist=Util.getrules(name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<rulelist.length;i++){
			TextIO.putln(rulelist[i].display());
			
		}

		initGUI();
	}

	private void initGUI() {
		try {
			if (this.name == null)
				return;
			
			String[] namelist = new String[rulelist.length];
			
			for(int i=0;i<rulelist.length;i++){
namelist[i]=rulelist[i].getName();
			}
			BoxLayout thisLayout = new BoxLayout(getContentPane(),
					javax.swing.BoxLayout.X_AXIS);
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel1 = new JPanel();
				BoxLayout jPanel1Layout = new BoxLayout(jPanel1,
						javax.swing.BoxLayout.Y_AXIS);
				jPanel1.setLayout(null);
				getContentPane().add(jPanel1);
				jPanel1.setPreferredSize(new java.awt.Dimension(221, 278));
				{
					ComboBoxModel cbox_rulesModel = new DefaultComboBoxModel(
							namelist);
					cbox_rules = new JComboBox();
					jPanel1.add(cbox_rules);
					cbox_rules.setModel(cbox_rulesModel);
					cbox_rules.setBounds(0, 7, 280, 29);
					cbox_rules.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							cbox_rulesActionPerformed(evt);
						}
					});
				}
			}
			{
				jPanel2 = new JPanel();
				GridLayout jPanel2Layout = new GridLayout(10, 1);
				jPanel2Layout.setColumns(1);
				jPanel2Layout.setHgap(5);
				jPanel2Layout.setVgap(5);
				jPanel2.setLayout(jPanel2Layout);
				getContentPane().add(jPanel2);
				{
					jLabel3 = new JLabel();
					jPanel2.add(jLabel3);
					jLabel3.setText("Guest Port");
				}
				{
					tfGuest = new JTextField();
					jPanel2.add(tfGuest);
				}
				{
					jLabel1 = new JLabel();
					jPanel2.add(jLabel1);
					jLabel1.setText("Host Port");
				}
				{
					tfHost = new JTextField();
					jPanel2.add(tfHost);
				}
				{
					bu_apply = new JButton();
					jPanel2.add(bu_apply);
					bu_apply.setText("Apply");
					bu_apply.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								bu_applyActionPerformed(evt);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
			}
			pack();
			setSize(400, 300);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void cbox_rulesActionPerformed(ActionEvent evt) {
		System.out.println("cbox_rules.actionPerformed, event="+evt);
		TextIO.putln(cbox_rules.getSelectedIndex());
		tfGuest.setText(this.rulelist[cbox_rules.getSelectedIndex()].getGuest()+"");
		tfHost.setText(this.rulelist[cbox_rules.getSelectedIndex()].getHost()+"");

		//TODO add your code for cbox_rules.actionPerformed
	}
	
	private void bu_applyActionPerformed(ActionEvent evt) throws IOException {
		System.out.println("bu_apply.actionPerformed, event="+evt);
		//TODO add your code for bu_apply.actionPerformed]
		int guest=Integer.parseInt((tfGuest.getText()));
		int host=Integer.parseInt((tfHost.getText()));

		Util.update_rule(this.name, this.rulelist[cbox_rules.getSelectedIndex()].getName(), guest,host);
	}

}
